package com.guanghua.brick.db;

import java.io.Serializable;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;

import com.guanghua.brick.BrickConstant;

/**
 * 操作hibernate的工具类，包括获取连接，以及一些辅助方法
 * @author leeon
 */
public class HBUtil {
	
	//获取数据库连接
	public static Session openSession() throws Exception {
		return openSession(null);
	}
	
	//根据某个特殊配置文件获取数据库连接
	public static Session openSession(String id) throws Exception {
		return BrickConstant.getHBSessionFactory().getSessionFactory(id).openSession();
	}
	
	//获取数据库连接
	public static Session getCurrentSession() throws Exception {
		return getCurrentSession(null);
	}
	
	//根据某个特殊配置文件获取数据库连接
	public static Session getCurrentSession(String id) throws Exception {
		return BrickConstant.getHBSessionFactory().getSessionFactory(id).getCurrentSession();
	}
	
	public static void save(Session session, Object[] o) {
		for (int i = 0; i < o.length; i ++)
			session.save(o[i]);
	}
	
	public static void saveOrUpdate(Session session, Object[] o) {
		for (int i = 0; i < o.length; i ++)
			session.saveOrUpdate(o[i]);	
	}
	
	public static void update(Session session, Object[] o) {
		for (int i = 0; i < o.length; i ++)
			session.update(o[i]);
	}

	
	public static void delete(Session session, Object[] o) {
		for (int i = 0; i < o.length; i ++)
			session.delete(o[i]);
	}
	
	public static void delete(Session session, Class cls, Serializable id) {
		session.delete(session.load(cls, id));
	}
	
	public static void delete(Session session, Class cls, Serializable[] ids) {
		for (int i = 0; i < ids.length; i ++)
			delete(session, load(session, cls, ids));
	}
		
	public static Object[] load(Session session, Class cls, Serializable[] ids) {
		Object[] ret = new Object[ids.length];
		for (int i = 0; i < ids.length; i ++)
			ret[i] = session.load(cls, ids[i]);
		return ret;
	}

	public static Object[] get(Session session, Class cls, Serializable[] ids) {
		Object[] ret = new Object[ids.length];
		for (int i = 0; i < ids.length; i ++)
			ret[i] = session.get(cls, ids[i]);
		return ret;
	}
	
	public static Query query(Session session, String hbl, PageInfo pager, OrderInfo order, Map<String, Object> filter) {
		Query q = session.createQuery(hbl + " " + (order!=null?order.getOrder():""));
		if (pager != null) q.setFirstResult(pager.getFirstResult()).setMaxResults(pager.getMaxResults());
		return q;
	}
	
	public static Query filter(Session session, Object collection, String hbl, PageInfo pager, OrderInfo order, Map<String, Object> filter) {
		Query q = session.createFilter(collection, hbl + " " + (order!=null?order.getOrder():""));
		if (pager != null) q.setFirstResult(pager.getFirstResult()).setMaxResults(pager.getMaxResults());
		return q;
	}
	
	public static SQLQuery queryBySQL(Session session, String sql, PageInfo pager, OrderInfo order, Map<String, Object> filter) {
		return null;
	}
	
}
